<template>
	<view>
		<u-sticky bgColor="#6492FF" offsetTop=0 customNavHeight=0>
			<navbox title="下级佣金设置"></navbox>
		</u-sticky>

		<view class="con">
			<view class="con__box">
				<view class="con__box__title">
					设置规则
				</view>
				<view class="con__box__gz">
					<view class="con__box__gz__top">
						<view class="con__box__gz__item">
							账户等级
						</view>
						<view class="con__box__gz__item">
							抽佣范围
						</view>
					</view>
					<view class="con__box__gz__row" v-for="(item,index) in RatioTop.ratios" :key="index">
						<view class="con__box__gz__item">
							{{item.customerType}}账号
						</view>
						<view class="con__box__gz__item">
							{{item.ratioMin.toFixed(1)}}~{{item.ratioMax.toFixed(1)}}%
							<text v-if="item.ratioFixed">或{{item.ratioFixed.toFixed(1)}}%</text>
						</view>
					</view>
				</view>
			</view>
			<view class="con__box">
				<view class="con__box__title">
					抽成比例
				</view>
				<view class="u-flex u-flex-y-center">
					<view class="con__box__text">
						当前抽成比例
					</view>
					<input type="number" placeholder-style="color:#999;" :value="Ratio" placeholder=""
						:style="'width:'+String(Ratio).length*10+'px;max-width:90%'" @input="onInputRatio"
						class="con__box__value" />
					<view class="con__box__unit">
						%
					</view>
				</view>
				<view class="con__box__footer">
					<u-button text="确认修改" @click="RatioSet" color="#6492FF" shape='circle'></u-button>
				</view>
			</view>
			<view class="" v-if="RatioShow.length>0">
				<view class="con__box" v-for="(item,index) in RatioShow" :key="index">
					<view class="u-flex u-flex-between con__box__top">
						<view class="u-flex u-flex-y-center">
							<image :src="item.productLogo" class="con__box__icon" mode="aspectFill"></image>
							<view class="con__box__title1">
								<view class="con__box__title1__name">
									{{item.productName}}
								</view>
								<view class="con__box__title1__sh">
									{{item.productMethod}}
								</view>
							</view>
						</view>

					</view>
					<view class="con__box__table">
						<view class="con__box__table__top">
							<view class="con__box__table__top__item">
								任务名称
							</view>
							<view class="con__box__table__top__item">
								我的价格
							</view>
							<view class="con__box__table__top__item">
								下级价格
							</view>
							<view class="con__box__table__top__item">
								我的收益
							</view>
							<view class="con__box__table__top__item">
								操作
							</view>
						</view>
						<view class="con__box__table__list" v-for="(item1,index1) in item.taskList" :key="index1">
							<view class="con__box__table__list__item1 es">
								{{item1.taskName}}
							</view>
							<view class="con__box__table__list__item1">
								{{item1.iPrice.toFixed(2)}}
							</view>
							<!-- <u-number-box v-model="value" integer @change="valChange"></u-number-box> -->
							<view class="con__box__table__list__item1">
								<view class="con__box__table__list__jian" @click="setnum(index,index1,'jian')">
									-
								</view>
								<input type="number" @input="onInput(index,index1)" class="con__box__table__list__inp"
									v-model="item1.childPrice" />
								<view class="con__box__table__list__jia" @click="setnum(index,index1,'jia')">
									+
								</view>
							</view>

							<view class="con__box__table__list__item1">
								{{item1.iIncome.toFixed(2)}}
							</view>
							<view class="con__box__table__list__item1" style="color: #6492FF;"
								@click="set(item, item1,index,index1)">
								修改
							</view>
						</view>
					</view>
				</view>
				<u-loadmore @loadmore="loadmore" :status="morestatus" />
			</view>
			<view class="" v-else style="margin-top: 200rpx;">
				<u-empty icon="/static/img/index/empty.png" mode="data" text="暂时没有数据"></u-empty>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		groupset
	} from '@/api/team.js'
	export default {
		data() {
			return {
				RatioTop: {},
				Ratio: 0,
				RatioShow: [],
				childId: '',
				value: 12.5,
				pageSize: 10,
				pageNum: 1,
				morestatus: 'loadmore',
				fw: {}
			};
		},
		methods: {
			onInputRatio(e) {
				// console.log(e.detail.value)
				this.Ratio = e.detail.value
				if (this.Ratio < 0) {
					this.Ratio = 0
				}
			},
			RatioSet() {
				let _this = this

				uni.showModal({
					title: '调整该选项，将对所有项目的抽佣调整为该比例',
					success(r) {
						if (r.confirm) {
							// 判断范围
							// console.log(_this.fw)
							// 先判断有误ratioFixed

							if (_this.Ratio > _this.fw.ratioMax && _this.Ratio != _this.fw.ratioFixed) {
								_this.$u.toast(_this.fw.customerType + "账号佣金比例不能超过" + _this.fw.ratioMax.toFixed(
									1) + '%')
								return
							}

							if (_this.childId) {
								// 单个设置
								groupset('oneRatioSet', 'GET', {
									childRatio: _this.Ratio,
									childId: _this.childId
								}).then(res => {
									if (res.data.code == 200) {
										_this.$u.toast('修改成功')
										_this.morestatus = 'loadmore'
										_this.pageNum = 1
										_this.prePage().init()
										_this.getlist1('oneRatioShow')
									} else {
										_this.$u.toast(res.data.msg)
									}
								})
							} else {
								groupset('groupRatioSet', 'GET', {
									childRatio: _this.Ratio
								}).then(res => {
									if (res.data.code == 200) {
										_this.$u.toast('修改成功')
										_this.morestatus = 'loadmore'
										_this.pageNum = 1
										_this.prePage().init()
										_this.getlist1('groupRatioShow')
									} else {
										_this.$u.toast(res.data.msg)
									}
								})
							}
						}
					}
				})

			},
			prePage(index) {
				let pages = getCurrentPages();
				let prePage = pages[pages.length - (index || 2)];
				return prePage.$vm;
			},
			onInput(index, index1) {
				// console.log(this.RatioShow[index].taskList[index1].childPrice, this.RatioShow[index].taskList[index1]
				// 	.iPrice)
				// 判断是否小于0
				if (this.RatioShow[index].taskList[index1].childPrice < 0) {
					this.RatioShow[index].taskList[index1].childPrice = 0
				}
				if (this.RatioShow[index].taskList[index1].childPrice > this.RatioShow[index].taskList[index1].iPrice) {
					this.RatioShow[index].taskList[index1].childPrice = this.RatioShow[index].taskList[index1].iPrice
					// console.log(this.RatioShow[index].taskList[index1].childPrice)
				}
				
				// console.log()
				this.RatioShow[index].taskList[index1].iIncome = (this.RatioShow[index].taskList[index1].iPrice - this
					.RatioShow[index].taskList[index1].childPrice)
				// this.RatioShow[index].taskList[index1].childPrice=this.RatioShow[index].taskList[index1].childPrice.toFixed(2)
				// this.RatioShow[index].taskList[index1].iIncome = this.RatioShow[index].taskList[index1].iIncome.toFixed(2)
			},
			setnum(index, index1, type) {
				if (type == 'jia') {
					if (this.RatioShow[index].taskList[index1].childPrice == this.RatioShow[index].taskList[index1]
						.iPrice) {
						this.$u.toast('不能再加了')
						return
					}
					// console.log(this.isDecimalZero(this.RatioShow[index].taskList[index1].childPrice))
					if (!this.isDecimalZero(this.RatioShow[index].taskList[index1].childPrice)) {
						this.RatioShow[index].taskList[index1].childPrice = Math.ceil(this.RatioShow[index].taskList[
								index1]
							.childPrice)
					} else {
						this.RatioShow[index].taskList[index1].childPrice++

					}
					this.RatioShow[index].taskList[index1].childPrice=this.RatioShow[index].taskList[index1].childPrice.toFixed(2)
				}
				if (type == 'jian') {
					if (this.RatioShow[index].taskList[index1].childPrice == 0) {
						this.$u.toast('不能再减了')
						return
					}
					if (!this.isDecimalZero(this.RatioShow[index].taskList[index1].childPrice)) {
						this.RatioShow[index].taskList[index1].childPrice = Math.trunc(this.RatioShow[index].taskList[
								index1]
							.childPrice)
					} else {
						this.RatioShow[index].taskList[index1].childPrice--

					}
					this.RatioShow[index].taskList[index1].childPrice=this.RatioShow[index].taskList[index1].childPrice.toFixed(2)
				}
				this.RatioShow[index].taskList[index1].iIncome = Number((this.RatioShow[index].taskList[index1].iPrice -
					this.RatioShow[index].taskList[index1].childPrice))
				// console.log(this.RatioShow[index].taskList[index1].iIncome)
			},
			isDecimalZero(num) {
				return num % 1 === 0;
			},
			set(item, item1, index, index1) {
				let form = {
					childPrice: item1.childPrice,
					productId: item.productId,
					taskId: item1.taskId,
					id: item1.fixPriceId
				}

				let _this = this
				uni.showModal({
					title: '确认修改下级价格?',
					success(r) {
						if (r.confirm) {
							// 判断下级佣金范围范围,最小金额
							let min = 100 - _this.fw.ratioMax
							let ratioFixed = 100 - _this.fw.ratioFixed
							if (item1.childPrice < min * item1.iPrice / 100 && item1.childPrice != ratioFixed *
								item1.iPrice / 100) {
								_this.$u.toast(_this.fw.customerType + "账号下级价格不能低于" + min * item1.iPrice / 100)
								return
							}
							if (_this.childId) {
								groupset('oneFixed', 'POST', {
									...form,
									childId: Number(_this.childId)
								}).then(res => {
									if (res.data.code == 200) {
										_this.$u.toast('修改成功')
										_this.RatioShow[index].taskList[index1].fixPriceId = res.data
											.data
									} else {
										_this.$u.toast(res.data.msg)
									}
								})
							} else {
								groupset('groupFixed', 'POST', form).then(res => {
									if (res.data.code == 200) {
										_this.$u.toast('修改成功')
									} else {
										_this.$u.toast(res.data.msg)
									}
								})
							}

						}
					}
				})
			},
			getlist(type) {
				groupset(type, 'POST', {
					pageSize: this.pageSize,
					pageNum: this.pageNum,
					childId: this.childId,
				}).then(res => {
					if (res.data.code == 200) {
						if (res.data.data.length == 0) {
							this.morestatus = 'nomore';
							return
						} else {
							this.morestatus = 'loadmore';
						}
						res.data.data.map(t=>{
							t.taskList.map(t1=>{
								t1.childPrice=t1.childPrice.toFixed(2)
							})
						})
						this.RatioShow = [...this.RatioShow, ...res.data.data]
						
					}
				})
			},
			getlist1(type) {
				groupset(type, 'POST', {
					pageSize: this.pageSize,
					pageNum: this.pageNum,
					childId: this.childId,
				}).then(res => {
					if (res.data.code == 200) {
						if (res.data.data.length == 0) {
							this.morestatus = 'nomore';
							return
						} else {
							this.morestatus = 'loadmore';
						}
						this.RatioShow = res.data.data
					}
				})
			},
			getTop(type) {
				groupset(type, 'POST', {
					childId: this.childId
				}).then(res => {
					uni.stopPullDownRefresh()
					if (res.data.code == 200) {
						this.RatioTop = res.data.data
						this.Ratio = res.data.data.factor.toFixed(1)
						// 判断当前账户在哪个范围
						// console.log(this.RatioTop)
						this.RatioTop.ratios.map(t => {
							if (t.customerTypeKey == uni.getStorageSync('ytbuser').accountType) {
								this.fw = t
							}
						})
					}
				})
			},
			getinit() {
				// 判断有无childId
				if (this.childId) {
					// 单个设置
					this.getTop('oneRatioTop')
					this.getlist('oneRatioShow')
				} else {
					// 团队设置
					this.getTop('groupRatioTop')
					this.getlist('groupRatioShow')

				}
			},
			loadmore() {
				if (this.morestatus == 'nomore') return
				this.morestatus = 'loading';
				this.pageNum = ++this.pageNum;

				if (this.childId) {
					// 单个设置
					this.getlist('oneRatioShow')
				} else {
					// 团队设置
					this.getlist('groupRatioShow')
				}
			}
		},
		onLoad(option) {
			this.Ratio = Number(option.Ratio).toFixed(1)
			this.childId = option.childId

			this.getinit()
		},
		onPullDownRefresh() {
			this.morestatus = 'loadmore'
			this.pageNum = 1
			this.RatioShow = []
			this.getinit()
		},
		onReachBottom() {
			this.loadmore()
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f6f6f6;
	}

	.con {
		padding: 25rpx 16rpx;

		&__box {
			background: #FFFFFF;
			border-radius: 14rpx;
			padding-bottom: 1rpx;
			margin-bottom: 25rpx;

			&__title {
				padding: 30rpx 40rpx;
				font-family: Source Han Sans SC;
				font-weight: bold;
				font-size: 33rpx;
				color: #3E3D3D;
				line-height: 33rpx;
			}

			&__gz {
				width: 636rpx;
				border-radius: 10rpx;
				margin-left: 40rpx;
				line-height: 60rpx;
				overflow: hidden;
				border: 1rpx solid rgba(212, 227, 255, .6);
				margin-bottom: 24rpx;

				&__top {
					background-color: rgba(184, 208, 255, .6);
					font-family: Source Han Sans SC;
					font-weight: bold;
					font-size: 26rpx;
					color: #3E3D3D;
					display: flex;
					align-items: center;
				}

				&__item {
					line-height: 60rpx;
					width: 50%;
					text-align: center;
				}

				&__row {
					background-color: rgba(246, 246, 246, .6);
					font-family: Source Han Sans SC;
					font-weight: 400;
					font-size: 26rpx;
					color: #3E3D3D;
					display: flex;
					align-items: center;
					border-top: 1rpx solid rgba(212, 227, 255, .6);
				}
			}

			&__text {
				font-family: Source Han Sans SC;
				font-weight: 400;
				font-size: 26rpx;
				color: #3E3D3D;
				line-height: 26rpx;
				margin-left: 40rpx;
				margin-right: 44rpx;
			}

			&__value {
				min-width: 36px;
				padding: 0 15px;
				height: 56rpx;
				border-radius: 28rpx;
				border: 1rpx solid #707070;
				text-align: center;
			}

			&__footer {
				padding: 25rpx 100rpx 38rpx;
			}

			&__unit {
				font-family: Source Han Sans SC;
				font-weight: bold;
				font-size: 33rpx;
				color: #3E3D3D;
				line-height: 33rpx;
				margin-left: 16rpx;
			}

			&__icon {
				width: 104rpx;
				height: 104rpx;
				margin-right: 30rpx;
				border-radius: 50%;
			}

			&__title1 {
				height: 70rpx;
				padding: 17rpx 0;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				&__name {
					font-family: SourceHanSansSC;
					font-weight: bold;
					font-size: 33rpx;
					color: #3E3D3D;
					line-height: 33rpx;
				}

				&__sh {
					font-family: SourceHanSansSC;
					font-weight: 400;
					font-size: 25rpx;
					color: #989898;
					line-height: 25rpx;
				}
			}

			&__top {
				padding: 36rpx 17rpx;
			}

			&__table {
				line-height: 60rpx;
				width: 672rpx;
				margin-left: 21rpx;
				border-radius: 10rpx;
				overflow: hidden;
				border: 1rpx solid #D4E3FF;
				margin-bottom: 36rpx;

				&__top {
					display: flex;
					align-items: center;
					justify-content: space-between;
					background: #D4E3FF;
					font-family: Source Han Sans SC;
					font-weight: bold;
					font-size: 26rpx;
					color: #3E3D3D;
					padding: 0 0 0 16rpx;

					&__item {
						width: 20%;
						display: flex;
						justify-content: center;
					}
				}

				&__list {
					padding: 0 0 0 16rpx;
					font-family: Source Han Sans SC;
					font-weight: 400;
					font-size: 27rpx;
					color: #3E3D3D;
					display: flex;
					align-items: center;
					border-top: 1rpx solid #D4E3FF;

					&__item1 {
						display: flex;
						justify-content: center;
						width: 20%;
						align-items: center;
						position: relative;
					}

					&__inp {
						width: 90rpx;
						height: 40rpx;
						border-radius: 21rpx;
						border: 1rpx solid #707070;
						text-align: center;
						font-size: 27rpx;
					}

					&__jian {
						height: 60rpx;
						display: flex;
						align-items: center;
						position: absolute;
						left: -16rpx;
						z-index: 1;
					}

					&__jia {
						height: 60rpx;
						display: flex;
						align-items: center;
						position: absolute;
						right: -16rpx;
						z-index: 1;
					}
				}
			}
		}
	}

	.es {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		display: block;
	}
</style>